package mq.rabbitmq.c_routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import mq.rabbitmq.ConnectionUtil;
import mq.rabbitmq.MqConstants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 路由模式：生产者
 * 路由模式的交换机类型是：direct
 *
 * @author shanzhenbo
 */
public class Producer {

	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();
		//声明交换机
		channel.exchangeDeclare(MqConstants.DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT);
		//声明队列
		channel.queueDeclare(MqConstants.DIRECT_QUEUE_INSERT, true, false, false, null);
		channel.queueDeclare(MqConstants.DIRECT_QUEUE_UPDATE, true, false, false, null);
		//队列绑定交换机
		channel.queueBind(MqConstants.DIRECT_QUEUE_INSERT, MqConstants.DIRECT_EXCHANGE, "insert");
		channel.queueBind(MqConstants.DIRECT_QUEUE_UPDATE, MqConstants.DIRECT_EXCHANGE, "update");

		String message = "路由模式 - insert message - routing key = insert";
		channel.basicPublish(MqConstants.DIRECT_EXCHANGE, "insert", null, message.getBytes());

		message = "路由模式 - update message - routing key = update";
		channel.basicPublish(MqConstants.DIRECT_EXCHANGE, "update", null, message.getBytes());

		System.out.println("发送消息结束");

		channel.close();
		connection.close();
	}

}
